package com.study.demo.repository;

import com.study.demo.dto.User;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<User, Serializable> {
    public User findByNameAndAge(String name, Integer age);
    public List<User> findByAge(Integer age, Pageable pageable);

    public User findFirstByOrderByAgeDesc();

    @Query("from User u where u.name=?1 and u.sex=?2")
    public User findByNameAndSex(String name, Integer sex);

    public User findByNameAndPassword(String name, String password);

    //修改密码
    @Transactional
    @Modifying
    @Query("update User u set u.password=?3 where u.name=?1 and u.password=?2")
    public void changePwd(String name, String password, String newPwd);
}
